package com.htstar.ovms.device.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htstar.ovms.common.core.constant.SecurityConstants;
import com.htstar.ovms.common.core.util.R;
import com.htstar.ovms.common.log.annotation.SysLog;

import com.htstar.ovms.common.security.annotation.Inner;
import com.htstar.ovms.common.security.util.SecurityUtils;
import com.htstar.ovms.device.api.entity.ExportRecord;
import com.htstar.ovms.device.service.ExportRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 导出记录表
 *
 * @author htxk
 * @date 2021-01-11 18:16:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/exportrecord" )
@Api(value = "exportrecord", tags = "导出记录表管理/Export record management")
public class ExportRecordController {

    private final ExportRecordService exportRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param exportRecord 导出记录表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getExportRecordPage( Page page,  ExportRecord exportRecord) {
        QueryWrapper<ExportRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExportRecord::getExportType,exportRecord.getExportType());
        wrapper.lambda().eq(ExportRecord::getUserId, SecurityUtils.getUser().getId());
        wrapper.lambda().last("and create_time >=(NOW() - interval 24 hour) ORDER BY create_time desc");
        return R.ok(exportRecordService.page(page, wrapper));
    }


    /**
     * 通过id查询导出记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询/Query by id", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(exportRecordService.getById(id));
    }

    /**
     * 新增导出记录表
     * @param exportRecord 导出记录表
     * @return R
     */
    @ApiOperation(value = "新增导出记录表/New export record table", notes = "新增导出记录表")
    @SysLog("新增导出记录表" )
    @PostMapping
    @Inner
    public Long save(@RequestBody ExportRecord exportRecord,@RequestHeader(SecurityConstants.FROM) String from) {
        exportRecordService.save(exportRecord);
        return exportRecord.getId();
    }

    /**
     * 修改导出记录表
     * @param exportRecord 导出记录表
     * @return R
     */
    @ApiOperation(value = "修改导出记录表/Modify the export record table", notes = "修改导出记录表")
    @SysLog("修改导出记录表" )
    @PutMapping
    @Inner
    public R updateById(@RequestBody ExportRecord exportRecord,@RequestHeader(SecurityConstants.FROM) String from) {
        return R.ok(exportRecordService.updateById(exportRecord));
    }

    /**
     * 通过id删除导出记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除导出记录表/Delete the export record table by id", notes = "通过id删除导出记录表")
    @SysLog("通过id删除导出记录表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_exportrecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(exportRecordService.removeById(id));
    }

}
